package service

import (
	"singo/model"
	"singo/serializer"
)

// OrganizationUpdateService 更新组织的服务
type OrganizationUpdateService struct {
	ID       string `form:"id" json:"id" binding:"required"`
	Name     string `form:"name" json:"name" binding:"max=30"`
	Intro    string `form:"intro" json:"intro"`
	Avatar   string `form:"avatar" json:"avatar" binding:""`
	ParentID string `form:"parent_id" json:"parent_id"`
	Head     string `form:"head" json:"head"`
	Score    uint   `form:"score" json:"score"`
}

// OrganizationUpdate 更新组织
func (service *OrganizationUpdateService) OrganizationUpdate() serializer.Response {
	var organization model.Organization
	err := model.DB.First(&organization, "uid = ?", service.ID).Error
	if err != nil {
		return serializer.Response{
			Status: 404,
			Msg:    "组织不存在",
			Error:  err.Error(),
		}
	}

	organization.Name = service.Name
	organization.Intro = service.Intro
	organization.Avatar = service.Avatar
	organization.Score = service.Score
	organization.Head = service.Head
	organization.ParentID = service.ParentID
	err = model.DB.Save(&organization).Where("uid = ?", service.ID).Error
	if err != nil {
		return serializer.Response{
			Status: 50002,
			Msg:    "组织保存失败",
			Error:  err.Error(),
		}
	}

	return serializer.Response{
		Data:   true,
		Msg:    "成功",
		Status: 0,
	}
}
